﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace STHPMS1.Common.C_Utils
{
    public class Algorithm
    {
        /// <summary>
        /// 求一个集合中与一个值最接近的两个数
        /// </summary>
        /// <param name="arr">必须先排序</param>
        /// <param name="val"></param>
        /// <param name="min"></param>
        /// <param name="max"></param>
        public static void GetMinMax(List<double> arr, double val, out double min, out double max)
        {
            min = -1;
            max = -1;
            double last = -1;

            if (arr.Count == 0) 
                return;
            if(arr.Count==1)
            {
                max=min = arr[0];
                return;
            }
            if(arr[0]>=val)
            {
                max = min = arr[0];
                return;
            }
            if (arr[arr.Count-1] <= val)
            {
                max = arr[arr.Count - 1];
                return;
            }
            foreach (var item in arr)
            {
                if (item > val)
                {
                    max = item;
                    min = last;
                    break;
                }

                last = item;
            }
        }
    }
}
